<!-- 正文开始 -->
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <!-- 表格 -->
      <table
        lay-filter="skuBenchmarkBookFormTable"
        id="skuBenchmarkBookFormTable"
      ></table>

      <div class="lay-btn-lt" style="margin-top: 40px">
        <button
          id="benchmarkBookAdd"
          type="button"
          class="layui-btn icon-btn color-reseda"
        >
          添加基准书
        </button>
        <button
          id="benchmarkBookConfirm"
          type="button"
          class="layui-btn icon-btn color-reseda"
        >
          确认选择
        </button>
      </div>

      <!-- 物料基准书表格 -->
      <table
        class="layui-table"
        id="skuBenchmarkBookInfo"
        lay-filter="skuBenchmarkBookInfo"
      ></table>
    </div>
  </div>
</div>

<script type="text/html" id="skuBenchmarkTableBar">
  <a
    class="layui-btn layui- layui-btn-xs"
    id="skuBenchmarkTableBarEdit"
    lay-event="edit"
    >编辑</a
  >
  <!-- {{#  if(d.status == 1){ }} -->
  <a class="layui-btn layui- layui-btn-xs" lay-event="audit">审核</a>
  <!-- {{#  } }} -->
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail"
    >详情</a
  >
  <!-- <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="drawing"
    >图纸管理</a
  > -->
</script>

<!-- js部分 -->
<script>
  layui.use(
    [
      "layer",
      "form",
      "table",
      "laydate",
      "admin",
      "laytpl",
      "tableTreeDj",
      "config",
    ],
    function () {
      var $ = layui.jquery;
      var layer = layui.layer;
      var form = layui.form;
      var table = layui.table;
      var config = layui.config;
      var admin = layui.admin;

      let formVal = {};
      const nowData = { ...parentData };
      let tempData = admin.getTempData("benData");
      console.log(123,tempData);
      
      if (nowData.typeName == "redio") {
        $("#skuBenchmarkTableBarEdit").hide();
        tempData.skuCode = tempData.customerSkuCode? tempData.customerSkuCode : tempData.skuCode;
        tempData.skuName = tempData.customerSkuName? tempData.customerSkuName : tempData.skuName;
        tempData.skuSpec = tempData.customerSkuSpec? tempData.customerSkuSpec : tempData.skuSpec;
      }

      if (tempData) {
        formVal = tempData;
      }
      console.log(111, formVal);

      // 渲染表格
      table.render({
        elem: "#skuBenchmarkBookFormTable",
        data: [{ ...formVal }],

        page: false,
        cellMinWidth: 100,
        cols: [
          [
            { type: "numbers", title: "序号" },
            {
              field: "skuCode",
              title: "sku料号",
            },
            { field: "skuName", title: "sku名称" },
            { field: "skuSpec", title: "规格码" },
            { field: "productTypeName", title: "产品类别" },
            { field: "materialNatureName", title: "产品性质" },
            { field: "productNatureName", title: "产品属性" },
          ],
        ],
        done: function () {
          $(
            "#benchmarkBookForm .layui-table-body, .layui-table-box, .layui-table-cell"
          ).css("overflow", "visible");
          admin.tableSelectChange("skuBenchmarkBookFormTable", "type");
        },
      });

      // 渲染表格
      table.render({
        id: "skuBenchmarkBookInfo",
        elem: "#skuBenchmarkBookInfo",
        url: config.base + "enterprise/benchmarkBook/admin/benchmarkBook/list",
        where: {
          skuCode: formVal.skuCode,
        },
        page: true,
        cellMinWidth: 100,
        cols: [
          [
            { type: "radio", title: "选择",},

            { type: "numbers", title: "序号", align: "center" },
            { field: "code", title: "基准书编号", align: "center", width: 160 },
            { field: "versionInterval", title: "基准书版本号", align: "center", width: 160, templet: function (d) {
              return d.skuCode + "-"+d.versionInterval;
            } },
            { field: "isBom", title: "BOM规划", align: "center" },
            { field: "isProcess", title: "工艺规划", align: "center" },
            { field: "isQuality", title: "质检标准", align: "center" },
            { field: "skuName", title: "成品验收标准", align: "center" },
            { field: "statusName", title: "状态", align: "center" },
            { field: "makePersonName", title: "创建人", align: "center" },
            { field: "createTime", title: "创建时间", align: "center" },
            {
              align: "center",
              toolbar: "#skuBenchmarkTableBar",
              title: "操作",
              width: 320,
              fixed: "right",
              templet: function (d) {
                return `<a class="layui-btn layui-btn-xs" id="skuBenchmarkTableBarEdit" lay-event="edit">编辑</a>`;
              },
            },
          ],
        ],
      });

      table.on("tool(skuBenchmarkBookInfo)", function (obj) {
        var data = obj.data;
        if (obj.event === "detail") {
          //详情
          admin.formOpen({
            area: ["80%", "80%"],
            title: "生成基准书",
            path: "components/res/benchmarkBook/benchmarkBookForm.html",
            success: function () {
              parentData.eventName = "detail";
              parentData.data = data;
            },
          });
        } else if (obj.event === "audit") {
          layer.confirm(
            "初审是否通过？",
            { icon: 3, title: "提示" },
            function (index) {
              admin.req(
                "enterprise/benchmarkBook/admin/benchmarkBook/benchmarkBookChange/" +
                  data.id +
                  "/" +
                  2,
                {},
                function (res) {
                  layer.msg("审核成功！", { icon: 1 });
                  table.reload("skuBenchmarkBookInfo");
                },
                "POST"
              );
            }
          );
        } else if (obj.event === "edit") {
          //详情
          admin.formOpen({
            area: ["80%", "80%"],
            title: "生成基准书",
            path: "components/res/benchmarkBook/benchmarkBookForm.html",
            success: function () {
              parentData.eventName = "edit";
              parentData.data = data;
            },
          });
        } else if (obj.event === "Production") {
          //生产
          admin.formOpen({
            type: 1,
            area: ["85%", "80%"],
            title: "添加生产计划",

            path: "components/productionPlan/productionPlanForm.html",
            success: function () {
              parentData.eventName = "add";
              parentData.productDetailId = data.productDetailId;
              parentData.type = "基准书生成";
            },
          });
        } else if (obj.event === "drawing") {
          //图纸管理
          admin.formOpen({
            area: ["80%", "80%"],
            title: "图纸管理",
            path: "components/res/draw/proDraw.html",
            success: function () {
              parentData.taskId = data.id;
            },
          });
        }
      });

      $("#benchmarkBookAdd").on("click", function () {
        admin.formOpen({
          area: ["80%", "80%"],
          title: "生成基准书",
          path: "components/res/benchmarkBook/benchmarkBookForm.html",
          success: function () {
            parentData.eventName = "add";
            parentData.data = {
              typeName: formVal.typeName,
              skuCode: formVal.skuCode,
              productDetailId: formVal.productDetailId,
              skuName: formVal.skuName,
              skuSpec: formVal.skuSpec,
              sourceName: formVal.sourceName,
              productTypeName: formVal.productTypeName,
              materialNatureName: formVal.materialNatureName,
              productAttrName: formVal.productAttrName,
            };
            parentData.type = "基准书列表";
          },
        });
      });
      $("#benchmarkBookConfirm").on("click", function () {
        let data = table.checkStatus("skuBenchmarkBookInfo").data[0];
        childData.obj = data;
        layer.close(nowData.zindex);
      });
    }
  );
</script>
